package cn.gjxblog.demo3;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 作用:测试java版Shiro,使用自定义数据源
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2018/7/25 21:05
 */
public class TestShiro3 {
    public static void main(String[] args) {
        //创建sessionFactory
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-md5Realm.ini");
        //创建安全管理器
        SecurityManager securityManager = factory.getInstance();
        //将安全管理器设置到工具类中
        SecurityUtils.setSecurityManager(securityManager);
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        //创建用户密码令牌
        UsernamePasswordToken token = new UsernamePasswordToken("xiaohong", "123456");

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        System.out.println(subject.isAuthenticated());
    }
}
